//
//  CancelReason.m
//  YaoHeAPP
//
//  Created by mac on 15/12/21.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "XiaDan.h"
#import "Controller.h"
#import "Define.h"
@implementation XiaDan

#define App_Frame_Height        [UIScreen mainScreen].bounds.size.height
#define App_Frame_Width         [[UIScreen mainScreen] bounds].size.width
#define Line_Low_Color   [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1]
//红色
#define  RedColor  [UIColor colorWithRed:216.0/255.0 green:12.0/255.0 blue:24.0/255.0 alpha:1]

#define txtFont [UIFont systemFontOfSize:15]



+(void)showPickerView:(UIView *)selfView withTitle:(NSString*)titleString classType:(NSString*)pinMing leiMing:(NSString*)leiMing oderID:(NSString*)oID unit:(NSString*)unit andSelectBlock:(void (^)(NSString *))selectRow
{
    //创建控制器
    XiaDan*cancel=[[XiaDan alloc]initWithFrame:selfView.bounds];
    cancel.SelectBlock=selectRow;
    cancel.titleStr=titleString;
    cancel.pric=oID;
    cancel.unit=unit;
    cancel.classTypeName=pinMing;
    cancel.leiMing=leiMing;
    [cancel setSubviews];
    [selfView addSubview:cancel];
}
//初始化大小
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeSelfAction)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    [self performSelector:@selector(showSelfAction) withObject:nil afterDelay:0];
    return self;
}

//设置，要弹出的视图上显示的东西
-(void)setSubviews
{
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(20,350, App_Frame_Width-40,220)];
     _bgView.backgroundColor =[UIColor whiteColor];
    _bgView.layer.shadowColor = [UIColor blackColor].CGColor;
    _bgView.layer.shadowOpacity = .3;
    _bgView.layer.shadowOffset = CGSizeMake(0, -1);
    _bgView.layer.masksToBounds=YES;
    _bgView.layer.cornerRadius=10;
    _bgView.layer.borderWidth=0.5;
    _bgView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [self addSubview:_bgView];
    //标签
    UILabel *titleL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _bgView.frame.size.width, 39)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.font = [UIFont systemFontOfSize:18];
    //titleL.textAlignment=NSTextAlignmentCenter;
    //titleL.textColor = [UIColor whiteColor];
    titleL.text = self.titleStr;
    [_bgView addSubview:titleL];
    
    //分割线图片
    UIImageView*lineImg=[[UIImageView alloc]init];
    lineImg.frame=CGRectMake(0, 39, _bgView.frame.size.width, 1);
    lineImg.backgroundColor=[UIColor lightGrayColor];
    [_bgView addSubview:lineImg];
    //报价label
    UILabel*bao_jia=[[UILabel alloc]init];
    bao_jia.text=@"报    价:";
    bao_jia.frame=CGRectMake(10, 60, 60, 20);
    [_bgView addSubview:bao_jia];
    
    //显示价格：（....元/吨）
    UILabel*price=[[UILabel alloc]init];
    if ([self.unit integerValue]==10) {
        price.text=[NSString stringWithFormat:@"%@元/吨",self.pric];
      }
    if ([self.unit integerValue]==20) {
        price.text=[NSString stringWithFormat:@"%@元/张",self.pric];
    }
    if ([self.unit integerValue]==30) {
        price.text=[NSString stringWithFormat:@"%@元/千克",self.pric];
    }
    if ([self.unit integerValue]==40) {
        price.text=[NSString stringWithFormat:@"%@元/件",self.pric];
    }
    price.frame=CGRectMake(70, 60,_bgView.frame.size.width-70, 20);
    price.textColor=[UIColor orangeColor];
    [_bgView addSubview:price];
    
    
    //成交量label
    UILabel*chengJiaoL=[[UILabel alloc]init];
    chengJiaoL.text=@"成交量:";
    //chengJiaoL. backgroundColor=[UIColor brownColor];
    chengJiaoL.frame=CGRectMake(10, 100, 60, 20);
    [_bgView addSubview:chengJiaoL];
    
     //输入成交量（.../吨）
    _dunText=[[UITextField alloc]init];
    _dunText.borderStyle=UITextBorderStyleRoundedRect;
    _dunText.frame=CGRectMake(70, 95,_bgView.frame.size.width-80,30);
    _dunText.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    _dunText.backgroundColor=[UIColor lightTextColor];
    _dunText.delegate=self;
    [_dunText addTarget:self action:@selector(keybordDissMiss) forControlEvents:UIControlEventEditingDidEndOnExit];
    [_bgView addSubview:_dunText];
    
    //吨
    dun=[[UILabel alloc]init];
    dun.font=font14;
    dun.textAlignment=NSTextAlignmentRight;
    dun.frame=CGRectMake(_dunText.frame.size.width-66, 0, 36, 30);
    if ([_unit integerValue]==10) {
        dun.text=@"吨";
    }
    if ([_unit integerValue]==20) {
        dun.text=@"张";
    }
    if([_unit integerValue]==30){
        dun.text=@"千克";
    }
    if ([_unit integerValue]==40) {
        dun.text=@"件";
    }
    dun.textColor=[UIColor lightGrayColor];
    [_dunText addSubview:dun];
    
    //图片视图
    UIImageView*imageView=[[UIImageView alloc]init];
    imageView.frame=CGRectMake(_dunText.frame.size.width-21,5,12,20);
    imageView.image=[UIImage imageNamed:@"ic_arraw"];
    [_dunText addSubview:imageView];
    imageView.transform = CGAffineTransformMakeRotation((M_PI*90.0/180.0));
    
    //选择单位的按钮
    unitBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    unitBtn.backgroundColor=[UIColor clearColor];
    unitBtn.frame=CGRectMake(_bgView.frame.size.width-10-66,95,66,30);
    unitBtn.selected=NO;
    [unitBtn addTarget:self action:@selector(clickSelectUnit) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:unitBtn];
    _tableAry=[[NSArray alloc]init];
    if ([self.classTypeName isEqualToString:@"板材"]) {
        _tableAry=@[@"张",@"吨",@"千克"];
    }
    if ([self.classTypeName isEqualToString:@"卷材"]) {
        _tableAry=@[@"吨"];
    }
    if ([self.classTypeName isEqualToString:@"管材"]) {
        _tableAry=@[@"件",@"吨",@"千克"];
    }
    if ([self.classTypeName isEqualToString:@"型材"]) {
        _tableAry=@[@"件",@"千克"];
    }
    if ([self.classTypeName isEqualToString:@"铝材"]) {
        if ([self.leiMing rangeOfString:@"卷"].location != NSNotFound) {
            _tableAry=@[@"吨"];
        }else{
            _tableAry=@[@"张",@"吨",@"千克"];
        }
    }
    

    //显示单位的view
    unitView=[[UIView alloc]init];
    unitView.hidden=YES;
    unitView.backgroundColor=bgVCcolor;
    unitView.frame=CGRectMake(_bgView.frame.size.width-10-33, 110,0,0);
    [_bgView addSubview:unitView];
        //创建表格
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,66, 90) style:UITableViewStylePlain];
    _tableView.showsVerticalScrollIndicator=NO;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.delegate=self;   _tableView.dataSource=self;
    _tableView.hidden=YES;
    [unitView addSubview:_tableView];

    
    /*for (NSInteger i=0 ; i<3; i++) {
        UIButton*danWeiBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        danWeiBtn.frame=CGRectMake(0, i*30, 40, 30);
        danWeiBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        danWeiBtn.backgroundColor=[UIColor orangeColor];
        [danWeiBtn setTitle:@"按钮" forState:UIControlStateNormal];
        [danWeiBtn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        [danWeiBtn setBackgroundImage:[UIImage imageNamed:@"nil"] forState:UIControlStateNormal];
        [danWeiBtn setBackgroundImage:[UIImage imageNamed:@"nil"] forState:UIControlStateHighlighted];
        [danWeiBtn addTarget:self action:@selector(danWeiBtn:) forControlEvents:UIControlEventTouchUpInside];
        [unitView addSubview:danWeiBtn];
    }*/
    
    
    //总额
   /* UILabel*zong_e=[[UILabel alloc]init];
    zong_e.text=@"总    额:";
    //zong_e.backgroundColor=[UIColor brownColor];
    zong_e.frame=CGRectMake(10, 140, 60, 20);
    [_bgView addSubview:zong_e];*/
    
  /*  //总价显示框
    _priceAll=[[UITextField alloc]init];
    _priceAll.frame=CGRectMake(70, 135, _bgView.frame.size.width-80, 30);
    _priceAll.borderStyle=UITextBorderStyleRoundedRect;
    [_bgView addSubview:_priceAll];
    
    //元标签
    UILabel*yuan=[[UILabel alloc]init];
    yuan.text=@"元";
    yuan.textColor=[UIColor lightGrayColor];
    yuan.frame=CGRectMake(_priceAll.frame.size.width-35, 0, 30, 30);
    [_priceAll addSubview:yuan];
    
    //覆盖在总价上边
    UIButton*lab=[[UIButton alloc]initWithFrame:CGRectMake(69, 135,  _bgView.frame.size.width-79, 32)];
    lab.backgroundColor=[UIColor clearColor];
    
    [_bgView addSubview:lab];
    */
    //选择免费质检的选择框
    selectZJ=[[UIButton alloc]init];
    selectZJ.selected=YES;
    selectZJ.frame=CGRectMake(10, _bgView.frame.size.height-79, 18, 18);
//    [selectZJ setBackgroundImage:[UIImage imageNamed:@"check_select"] forState:UIControlStateNormal];
    [selectZJ setImage:[UIImage imageNamed:@"check_select"] forState:UIControlStateNormal];
    [selectZJ addTarget:self action:@selector(clickChange) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:selectZJ];
    
    //申请免费质检的label
    UILabel*sqmfzj=[[UILabel alloc]init];
    sqmfzj.text=@"申请免费质检";
    sqmfzj.frame=CGRectMake(32, _bgView.frame.size.height-80, _bgView.frame.size.width-32, 20);
    [_bgView addSubview:sqmfzj];

    //确定按钮
    UIButton *sure = [UIButton buttonWithType:UIButtonTypeCustom];
    sure.backgroundColor = [UIColor whiteColor];
    sure.layer.borderWidth = 0.5;
    sure.layer.borderColor =[UIColor lightGrayColor].CGColor;
    sure.frame = CGRectMake(0,_bgView.frame.size.height-40, _bgView.frame.size.width, 40);
    [sure setTitle:@"确定" forState:UIControlStateNormal];
    [sure setTitleColor:RedColor forState:UIControlStateNormal];
    [sure addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:sure];
}

#pragma mark------------单元格tableView的代理协议方法
//返回有多少个分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//返回每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableAry.count;
}
//返回每一行单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}

//设置cell上的要显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
    cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.backgroundColor=[UIColor grayColor];
            //标签
    UILabel*label=[[UILabel alloc]init];
    label.frame=CGRectMake(0, 0, 66, 29);
    label.font=[UIFont systemFontOfSize:14];
    label.backgroundColor=[UIColor groupTableViewBackgroundColor];
    
    label.tag=500;
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor blackColor];
    [cell addSubview:label];
}
    UILabel*unitL=(UILabel*)[cell viewWithTag:500];
    unitL.text=_tableAry[indexPath.row];
    return cell;
}

//选中单元格时执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    dun.text=_tableAry[indexPath.row];
    [self cellRemove];
}
-(void)cellRemove
{
    [UIView animateWithDuration:0.5 animations:^{
        _tableView.hidden=YES;
        unitView.hidden=YES;
        unitView.frame=CGRectMake(_bgView.frame.size.width-10-33, 110,0,0);
    } completion:^(BOOL finished) {
    }];
}



//点击选择是否申请免费质检
-(void)clickChange
{
    if (selectZJ.selected==YES) {
        [selectZJ setImage:[UIImage imageNamed:@"check_normal"] forState:UIControlStateNormal];
        selectZJ.selected=NO;
        return;
    }
    if (selectZJ.selected==NO){
        [selectZJ setImage:[UIImage imageNamed:@"check_select"] forState:UIControlStateNormal];
        selectZJ.selected=YES;
        return;
    }
}

//点击确定按钮执行的方法
-(void)sure
{
    NSString*str=@"";
    NSString*chengJiaoUnit;
    if (selectZJ.selected==YES) {
        str=@"1";
    }
    else{
       str=@"0";
    }
    if (_dunText.text.length==0) {
        ALERTSHOW(@"请输入成交量");
        return;
    }
    if ([dun.text isEqualToString:@"件"]) {
        chengJiaoUnit=@"40";
    }
    if ([dun.text isEqualToString:@"千克"]) {
        chengJiaoUnit=@"30";
    }
    if ([dun.text isEqualToString:@"张"]) {
        chengJiaoUnit=@"20";
    }
    if ([dun.text isEqualToString:@"吨"]) {
        chengJiaoUnit=@"10";
    }
    NSArray*array=[[NSArray alloc]initWithObjects:_dunText.text,str,chengJiaoUnit,nil];
    _stringRow=[array componentsJoinedByString:@"|"];
    self.SelectBlock(_stringRow);
    [self removeSelfAction];
}
-(void)clickSelectUnit
{
    [UIView animateWithDuration:0.25 animations:^{
        unitView.hidden=NO;
        unitView.frame=CGRectMake(_bgView.frame.size.width-10-66, 65,66,90);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:1 animations:^{
            _tableView.hidden=NO;
        }];
    }];
}

-(void)btn{  //按钮
   
}

//展示出选择器
- (void)showSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0, -240);
    } completion:^(BOOL finished) {
        
    }];
}
//移除选择器
- (void)removeSelfAction
{
    [UIView animateWithDuration:0.2 animations:^{
        _bgView.transform  = CGAffineTransformIdentity;
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
}

#pragma tap  delegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint point = [gestureRecognizer locationInView:gestureRecognizer.view];
    if (CGRectContainsPoint(_bgView.frame, point)) {
        return NO;
    }
    return YES;
}

-(void)keybordDissMiss
{
    return;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.25 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0,-320);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
    [UIView animateWithDuration:0.25 animations:^{
        _bgView.transform = CGAffineTransformMakeTranslation(0,-240);
    } completion:^(BOOL finished) {
        
    }];
}




//根据输入的吨数的变化来改变下方的价格
-(void)textChange:(NSNotification*)notification
{
    float jia_Ge=[self.pric floatValue];
    float danWei=[_dunText.text floatValue];
    NSString*str=[NSString stringWithFormat:@"%.2f",jia_Ge*danWei];
    _priceAll.text=str;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];    //主要是[receiver resignFirstResponder]在哪调用就能把receiver对应的键盘往下收
    return YES;
}


@end



